/********************************************************************
	created:	2009:12:8   20:24:25
	filename: 	customsplitwnd.cpp
	author:		zouyf
	describe:	
*********************************************************************/
#include "stdafx.h"
#include "customsplitwnd.h"

void FCustomSplitWnd::Init(IApplication* pApp)
{
	m_pApp = pApp;
}

int FCustomSplitWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	IPlayListManager *pPlayListManager = m_pApp->GetPlayListManager();
	m_playListListBox.Init(m_pApp);
	m_playListListBox.Create(m_hWnd, rcDefault, L"", WS_CHILD | WS_VISIBLE | LBS_OWNERDRAWFIXED | LBS_NOTIFY);
	m_playListListBox.SetDlgCtrlID(IDC_LISTBOX_PLAYLIST);
	SetSplitterPane(0, m_playListListBox.m_hWnd);
	int nCount = pPlayListManager->GetPlayListCount();
	for (int i = 0; i < nCount; ++i)
	{
		std::wstring name = pPlayListManager->GetPlayList(i)->GetName();
		m_playListListBox.AddString(name.c_str());
	}
	m_playListListBox.SetCurSel(0);

	m_songListBox.Init(m_pApp);
	m_songListBox.Create(m_hWnd, rcDefault, L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_OWNERDRAWFIXED | LBS_NOTIFY);
	m_songListBox.SetDlgCtrlID(IDC_LISTBOX_SONG);
	SetSplitterPane(1, m_songListBox.m_hWnd);
	IPlayList* pPlayList = pPlayListManager->GetCurPlayList();
	int nSongCount = pPlayList->GetSongCount();
 	for (int i = 0; i < nSongCount; ++i)
	{
		m_songListBox.AddString(pPlayList->GetSong(i)->GetSongName());
	}
	m_songListBox.SetCurSel(0);
	SetMsgHandled(FALSE);
	return FALSE;
}

LRESULT FCustomSplitWnd::DispatchMsg(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT result = FALSE;
	switch(uMsg)
	{
	case WM_USER_SETSONGLISTIDX:
		{
			m_songListBox.SetCurSel(wParam);
			::SendMessage(m_songListBox.m_hWnd, WM_COMMAND, MAKEWPARAM(IDC_LISTBOX_SONG, LBN_SELCHANGE), (LPARAM)m_hWnd);
		}
		break;
	}

	return result;
}

LRESULT FCustomSplitWnd::OnChangePlayList(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	IPlayListManager *pPlayListManager = m_pApp->GetPlayListManager();
	IPlayList *pPlayList = pPlayListManager->GetCurPlayList();
	m_songListBox.ResetContent();
	for (int i = 0; i < pPlayList->GetSongCount(); ++i)
	{
		m_songListBox.AddString(pPlayList->GetSong(i)->GetSongName());
	}

	return FALSE;
}

void FCustomSplitWnd::OnDbClickItem(UINT uNotifyCode, int nID, CWindow wndCtl)
{
	if (LBN_DBLCLK == uNotifyCode)
	{
		if (nID == IDC_LISTBOX_SONG)
		{
			int nIdx = m_songListBox.GetCurSel();
			IPlayControler *pPlayCtrler = m_pApp->GetPlayControler();
			IPlayListManager *pPlayListManager = m_pApp->GetPlayListManager();
			pPlayListManager->GetCurPlayList()->SetCurPlaySong(nIdx);
			pPlayCtrler->Play();
			::SendMessage(::GetParent(m_hWnd), WM_USER_LYCPLAY, NULL, NULL);
		}
		else if (nID == IDC_LISTBOX_PLAYLIST)
		{
			int nIdx = m_playListListBox.GetCurSel();
			IPlayListManager *pPlayListManager = m_pApp->GetPlayListManager();
			pPlayListManager->SetCurPlayList(pPlayListManager->GetPlayList(nIdx));
			::SendMessage(m_hWnd, WM_USER_CHANGEPLAYLIST, NULL, NULL);
		}
		
		//m_PlayState = PS_Playing;
	}
	SetMsgHandled(FALSE);
}
